package com.tongji.seller.view
{
	import com.adobe.serialization.json.JSON;
	import com.tongji.seller.events.AddStoreItemEvent;
	import com.tongji.seller.events.DeleteProductEvent;
	import com.tongji.seller.events.GetStoreItemInfoEvent;
	import com.tongji.seller.events.GetStoreItemsEvent;
	import com.tongji.seller.events.utilsEvent.BubblesEvent;
	import com.tongji.seller.events.utilsEvent.ChangeStepEvent;
	import com.tongji.seller.events.utilsEvent.PageChangeEvent;
	import com.tongji.seller.model.StoreProductModel;
	import com.tongji.seller.model.StoreSceneModel;
	import com.tongji.seller.model.UserModel;
	import com.tongji.seller.utils.Matting;
	import com.tongji.seller.utils.PageTools;
	import com.tongji.seller.utils.PopUpAdmin;
	import com.tongji.seller.vo.Picture;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.graphics.codec.PNGEncoder;
	import mx.utils.Base64Encoder;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 *
	 * @author caddy
	 * @date 2011-11-26
	 */
	public class SecondStepViewMediator extends Mediator
	{
		[Inject]
		public var view:SecondStepView;
		
		[Inject]
		public var productModel:StoreProductModel;
		
		[Inject]
		public var userModel:UserModel;
		
		private var productPageIndex:int = 1;
				
		public var productStateVar:int = 0;
		private var index:Vector.<int>;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.productsList, "dataProvider", productModel, "allProductArray");	
			BindingUtils.bindProperty(view.productsImportList, "dataProvider", productModel, "storeProductArray");
			
			index = new Vector.<int>();

			addViewListener(BubblesEvent.POP_UP_IMAGE_WIN, popUpAddImageHandler, BubblesEvent);
			addViewListener(BubblesEvent.SELECT_PRODUCT, selectProductHandler, BubblesEvent);
			addViewListener(BubblesEvent.REMOVE_PRODUCT, removeProductHandler, BubblesEvent);
			//addViewListener(BubblesEvent.DELETE_PRODUCT, deleteProductHandler, BubblesEvent);

			eventMap.mapListener(view.nextStepBtn, MouseEvent.CLICK, changeToThird, MouseEvent);
			eventMap.mapListener(view.importBtn, MouseEvent.CLICK, importProductHandler, MouseEvent);
			eventMap.mapListener(view.searchProductBtn, MouseEvent.CLICK, searchProductHandler, MouseEvent);			
			eventMap.mapListener(view.productPageBar, PageChangeEvent.PAGE_INDEX_CHANGE, changeProductPageBar, PageChangeEvent);
			eventMap.mapListener(view.importProductPageBar, PageChangeEvent.PAGE_INDEX_CHANGE, changeSavedProductPageBar, PageChangeEvent);
			
			addContextListener(GetStoreItemsEvent.UPDATE_ALL_ITEMS, createPageBar, GetStoreItemsEvent);
			addContextListener(GetStoreItemInfoEvent.UPDATE_STORE_ITEM, createSavedPageBar, GetStoreItemInfoEvent);
			
			var page:PageTools = new PageTools(productModel.productCount, 10, 6, productPageIndex);
			view.productPageBar.createDataProvider([page.nums, page.pageArray, page.pageCount, productPageIndex]);	
			
			var page2:PageTools = new PageTools(productModel.savedProductCount, 10, 3, productPageIndex);
			view.importProductPageBar.createDataProvider([page2.nums, page2.pageArray, page2.pageCount, productPageIndex]);
		}
		
		private function removeProductHandler(event:BubblesEvent):void
		{
			
			for each(var obj:Object in productModel.savedProductArray)
			{
				if(obj.detailUrl == event.object.num_iid)
				{
					productModel.deletePro = obj;
					Alert.yesLabel = "确定删除";
					Alert.noLabel = "取消删除";
					Alert.show("是否删除已保存过得宝贝?", "", 3, view, closeHandler);
				}
				else if(obj.detailUrl == event.object.detailUrl)
				{
					productModel.deletePro = obj;
					Alert.yesLabel = "确定删除";
					Alert.noLabel = "取消删除";
					Alert.show("是否删除已保存过得宝贝?", "", 3, view, closeHandler);
				}
			}
			
			for each(var obj2:Object in productModel.tempSelectedPro)
			{
				if(obj2.num_iid == event.object.num_iid)
				{
					productModel.tempSelectedPro.removeItemAt(productModel.tempSelectedPro.getItemIndex(obj2));
				}
			}
		}
		
		private function closeHandler(event:CloseEvent):void
		{
			if(event.detail == Alert.YES)
			{
				dispatch(new DeleteProductEvent(DeleteProductEvent.DELETE_PRODUCT, productModel.deletePro));
			}
		}
		
		/**
		 * 弹出添加宝贝图片的浮云框 
		 * @param event
		 * 
		 */		
		private function popUpAddImageHandler(event:BubblesEvent):void
		{
			productModel.currentProduct = event.object;
			productModel.getImageByProduct(event.object);
			/*productModel.imageList = new ArrayCollection();
			var str:String = "assets/images/image.png";
			
			if (event.object.ItemImgList != null)
			{
				var imgOne:Object = new Object();
				imgOne.id = 1;
				imgOne.edited = 0;
				imgOne.newurl = null;
				if (event.object.ItemImgList.imgOne != null)
				{
					imgOne.url = event.object.ItemImgList.imgOne;
					imgOne.edited = 1;
				}
				else
					imgOne.url = str;
				
				productModel.imageList.addItem(imgOne);
				
				var imgTwo:Object = new Object();
				imgTwo.id = 2;
				imgTwo.edited = 0;
				imgTwo.newurl = null;
				if (event.object.ItemImgList.imgTwo != null)
				{
					imgTwo.url = event.object.ItemImgList.imgTwo;
					imgTwo.edited = 1;
				}
					
				else
					imgTwo.url = str;
					
				productModel.imageList.addItem(imgTwo);
				
				var imgThree:Object = new Object();
				imgThree.id = 3;
				imgThree.edited = 0;
				imgThree.newurl = null;
				if (event.object.ItemImgList.imgThree != null)
				{
					imgThree.url = event.object.ItemImgList.imgThree;
					imgThree.edited = 1;
				}
				else
					imgThree.url = str;
					
				productModel.imageList.addItem(imgThree);
			}
			else
			{
				var imgOne1:Object = new Object();
				imgOne1.id = 1;
				imgOne1.edited = 0;
				imgOne1.newurl = null;
				imgOne1.url = str;
				
				productModel.imageList.addItem(imgOne1);
				
				var imgTwo1:Object = new Object();
				imgTwo1.id = 2;
				imgTwo1.edited = 0;
				imgTwo1.newurl = null;
				imgTwo1.url = str;
				
				productModel.imageList.addItem(imgTwo1);
				
				var imgThree1:Object = new Object();
				imgThree1.id = 3;
				imgThree1.edited = 0;
				imgThree1.newurl = null;
				imgThree1.url = str;
				
				productModel.imageList.addItem(imgThree1);
			}
*/
			PopUpAdmin.popupCenterWindow(PopUpAdmin.ADD_IMAGE_TITLE_WIN, view.parent, mediatorMap);
		}
		
		/**
		 * 选择某一宝贝 
		 * @param event
		 * 
		 */		
		private function selectProductHandler(event:BubblesEvent):void
		{
			/*view.productsList.selectedIndices.push(productModel.allProductArray.getItemIndex(event.object));
			view.productsList.selectedIndices.push(2);*/
			productModel.tempSelectedPro.addItem(event.object);
			index.push(productModel.allProductArray.getItemIndex(event.object));
			view.productsList.selectedIndices = index;
			BindingUtils.bindProperty(view.productsList, "dataProvider", productModel, "allProductArray");
			var locate:int = productModel.selectedProductArray.getItemIndex(event.object);
			if (locate == -1)
			{
				productModel.selectedProductArray.addItem(event.object);
			}
			for (var i:int = 0; i <  productModel.selectedProductArray.length; i++)
			{
				for (var j:int = i + 1; j <  productModel.selectedProductArray.length; j++)
				{
					if ( productModel.selectedProductArray.getItemAt(i).pic_url ==  productModel.selectedProductArray.getItemAt(j).pic_url)
					{
						productModel.selectedProductArray.removeItemAt(j);
						j--;
					}
				}
			}
			
		}
		
		/**
		 * 跳转至第三步 
		 * @param event
		 * 
		 */		
		private function changeToThird(event:MouseEvent):void
		{
			if (productModel.checkStoreProduct())
				dispatch(new ChangeStepEvent(ChangeStepEvent.TO_THIRD_STEP));
			else
				Alert.show("您还没有上传过图片的宝贝");
		}
		
		/**
		 * 导入选择的宝贝 
		 * @param event
		 * 
		 */		
		private function importProductHandler(event:MouseEvent):void
		{
			var obj:Object;
			var itemList:Object = [];
			for(var i:int = 0; i < productModel.tempSelectedPro.length; i++)
			{
				obj = new Object();
				obj.pro_url = productModel.tempSelectedPro.getItemAt(i).pic_url;
				obj.itemId = productModel.tempSelectedPro.getItemAt(i).num_iid;
				itemList[i] = obj;				
			}
			var importProduct:Object = new Object();
			importProduct.uid = userModel.currentUserID;
			importProduct.itemList = itemList;
			var str:String = JSON.encode(importProduct);
			dispatch(new AddStoreItemEvent(AddStoreItemEvent.ADD_STORE_ITEM_INFO_LIST, 0, 0, null, str));
			BindingUtils.bindProperty(view.productsImportList, "dataProvider", productModel, "selectedProductArray");
			productModel.tempSelectedPro = new ArrayCollection();
		}
		
		/**
		 * 搜索宝贝 
		 * @param event
		 * 
		 */		
		private function searchProductHandler(event:MouseEvent):void
		{
			productStateVar = 1;
			dispatch(new GetStoreItemsEvent(GetStoreItemsEvent.GET_ALL_ITEMS, "亿好旗舰店", view.searchProductText.text, 1, 6));
		}
		
		/**
		 * 重新设置宝贝列表的页数 
		 * @param event
		 * 
		 */		
		private function changeProductPageBar(event:PageChangeEvent):void
		{
			productPageIndex = event.pageIndex;
			switch(productStateVar)
			{
				case 0:
					dispatch(new GetStoreItemsEvent(GetStoreItemsEvent.GET_ALL_ITEMS, "亿好旗舰店", "", productPageIndex, 6));
					break;
				case 1:
					dispatch(new GetStoreItemsEvent(GetStoreItemsEvent.GET_ALL_ITEMS, "亿好旗舰店", view.searchProductText.text, productPageIndex, 6));
					break;
				default:
					break;	
			}
		}
		
		/**
		 * 重新设置已保存宝贝的页数
		 * @param event
		 * 
		 */		
		private function changeSavedProductPageBar(event:PageChangeEvent):void
		{
			productPageIndex = event.pageIndex;
			dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_ITEM, userModel.currentUserID, productPageIndex, 3));
		}
		
		/**
		 * 更新宝贝列表的页数  
		 * @param event
		 * 
		 */		
		private function createPageBar(event:GetStoreItemsEvent):void
		{
			var page:PageTools = new PageTools(productModel.productCount, 10, 6, productPageIndex);
			view.productPageBar.createDataProvider([page.nums, page.pageArray, page.pageCount, productPageIndex]);
			BindingUtils.bindProperty(view.productsList, "dataProvider", productModel, "allProductArray");
			index = new Vector.<int>();
		}
		
		/**
		 * 更新已保存宝贝的页数
		 * @param event
		 * 
		 */		
		private function createSavedPageBar(event:GetStoreItemInfoEvent):void
		{
			var page:PageTools = new PageTools(productModel.savedProductCount, 10, 3, productPageIndex);
			view.importProductPageBar.createDataProvider([page.nums, page.pageArray, page.pageCount, productPageIndex]);
			BindingUtils.bindProperty(view.productsImportList, "dataProvider", productModel, "storeProductArray");	
			index = new Vector.<int>();
		}
	}
}